package com.web.kongjian.ao.impl;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import cn.mysteel.util.FolderUtils;
import cn.mysteel.util.ObjectUtils;
import cn.mysteel.util.StringUtils;

import com.base.kongjian.entity.FooterPrint;
import com.base.kongjian.entity.Message;
import com.base.kongjian.entity.RealInform;
import com.base.kongjian.entity.WorkShareRec;
import com.base.kongjian.service.FooterPrintService;
import com.base.kongjian.service.MessageService;
import com.base.kongjian.service.RealInformService;
import com.base.kongjian.service.WorkShareRecService;
import com.base.user.entity.Member;
import com.base.user.service.BlackFriendsService;
import com.base.user.service.FriendsService;
import com.base.user.service.MemberService;
import com.web.kongjian.ao.KongjianAo;
import com.web.kongjian.ao.utils.KongjianUtils;
import com.web.kongjian.ao.utils.PictureUtils;
import com.web.user.member.utils.CookieUtils;

@Service("kongjianAo")
public class KongjinaAoImpl implements KongjianAo
{
	@Autowired
	private WorkShareRecService workShareRecService;
	@Autowired
	private FooterPrintService footerPrintService;
	@Autowired
	private BlackFriendsService blackFriendsService;
	@Autowired
	private FriendsService friendsService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private RealInformService realInformService;
	@Autowired
	private MessageService messageService;

	@Override
	public void deleteWorks(HttpServletRequest request, String type,long workId)
	{
		
		String str = CookieUtils.getCookieValue (request ,"member_id");
		
		long sharerId = 0L;
		
		if(!StringUtils.isTrimEmpty (str))
		{
			sharerId = Long.parseLong (str);
		}
		try
		{
			type = new String (type.getBytes ("iso8859-1"),"gbk");
		}catch (UnsupportedEncodingException e)
		{
		}
		Map<String, Object> params = new HashMap<String,Object>();
		if(type.equals ("΢��"))
		{
			params.put ("workKind" ,1);
		}
		
		params.put ("sharerId" ,sharerId);
		params.put ("type" ,2);
		params.put ("workId" ,workId);
		
		WorkShareRec wsRec = workShareRecService.getWSRByMap (params);
		
		if(ObjectUtils.notNull (wsRec))
		{
			workShareRecService.deleteWSRById (wsRec.getId ());
		}
	}

	@Override
	public void deleteVisitor(HttpServletRequest request, long visitor_MId)
	{
		
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("memberId" ,memberId);
		params.put ("visitor_MId" ,visitor_MId);
		
		FooterPrint fp = footerPrintService.getFPByMap (params);
		
		if(ObjectUtils.notNull (fp))
		{
			footerPrintService.deleteFPById (fp.getId ());
		}
	}

	@Override
	public void intoBlackFriend(HttpServletRequest request, long visitor_MId)
	{
		deleteVisitor(request,visitor_MId);
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		blackFriendsService.intoBlack (memberId ,visitor_MId);
	}

	@Override
	public void deleteFriend(HttpServletRequest request, long friend_Id)
	{
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		
		friendsService.deleteFriends (memberId ,friend_Id);
	}

	@Override
	public void blackList(HttpServletRequest request, long friend_Id)
	{
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		blackFriendsService.intoBlack (memberId ,friend_Id);
	}

	@Override
	public void edit(HttpServletRequest request, Member m)
	{
		Assert.notNull (m);
		Member member = memberService.getMemberById (m.getId ());
		
		if(ObjectUtils.notNull (member))
		{
			member.setNickName (m.getNickName ());
			member.setSchool (m.getSchool ());
			member.setBirthDay (m.getBirthDay ());
			member.setGender (m.getGender ());
			member.setAge (m.getAge ());
			
			memberService.updateMember (member);
		}
	}

	@Override
	public void headerEdit(HttpServletRequest request, MultipartFile photoFile)
	{
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		Member m = memberService.getMemberById (memberId);
		
		if(ObjectUtils.notNull (m))
		{
			
		String myappPath = request.getSession().getServletContext().getRealPath("/");
		
		try
		{
			if (request instanceof MultipartHttpServletRequest)
			{
				DiskFileItem df = (DiskFileItem) ((CommonsMultipartFile) photoFile).getFileItem();
				String sFileName = System.currentTimeMillis() + ".jpg";
				String uploadPath = "/pic/header" + "/";
				FolderUtils.mkdirs(myappPath+uploadPath);
				//ImageUtils.createThumbnail(df.getStoreLocation().getAbsolutePath(), myappPath+uploadPath + sFileName,180, 86);
				PictureUtils.copyPicture (df.getStoreLocation().getAbsolutePath() ,myappPath+uploadPath + sFileName,180, 86);
				String realPath = "http://"+ request.getServerName()+ ":"+ request.getServerPort()+ request.getContextPath();
				m.setHeadUrl  (realPath+uploadPath + sFileName);
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		memberService.updateMember (m);
		}
	}

	@Override
	public void signEidt(HttpServletRequest request, String content)
	{
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		
		Member member = memberService.getMemberById (memberId);
		
		if(ObjectUtils.notNull (member))
		{
			member.setSign (content);
			memberService.updateMember (member);
		}
	}

	@Override
	public void realInformEdit(HttpServletRequest request, RealInform r)
	{
		
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		
		RealInform realInform = realInformService.getRIByMemberId (memberId);
		
		if(ObjectUtils.notNull (realInform))
		{
			realInform.setName (r.getName ());
			realInform.setPhone (r.getPhone ());
			realInform.setSfNum (r.getSfNum ());
			realInform.setHobby (r.getHobby ());
			realInform.setAddress (r.getAddress ());
			
			realInformService.update (realInform);
		}else
		{
			r.setMemberId (memberId);
			realInformService.saveRI (r);
		}
		
	}

	@Override
	public String passValidate(HttpServletRequest request, String old)
	{
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		
		Member m = memberService.getMemberById (memberId);
		
		if(ObjectUtils.notNull (m))
		{
			String passString = m.getPassword ();
			
			if(old.equals (passString))
			{
				return null;
			}else
			{
				return "*ԭ������д����";
			}
		}
		
		return "*ϵͳ����";
	}

	@Override
	public void passwordEdit(HttpServletRequest request, String new_password)
	{
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		
		Member m = memberService.getMemberById (memberId);
		
		if(ObjectUtils.notNull (m))
		{
			m.setPassword (new_password);
			
			memberService.updateMember (m);
		}
	}

	@Override
	public void deleteBlackFriend(HttpServletRequest request, long friend_id)
	{
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		blackFriendsService.outBlack (memberId ,friend_id);
	}

	@Override
	public void addFriendFromBlack(HttpServletRequest request, long friend_id)
	{
		
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		
		blackFriendsService.outBlack (memberId ,friend_id);
		Message m = new Message ();
		m.setMemberId (friend_id);
		m.setSend_MId (memberId);
		Member member  = memberService.getMemberById (memberId );
		
		if(ObjectUtils.notNull (member))
		{
			m.setContent (member.getNickName ()+"�������Ϊ����");
			m.setType (2);
			messageService.sendMessage (m);
		}
	}
}
